package com.dataseek.iot.device.manager.handler;

import com.dataseek.iot.mysql.entity.MqttTopic;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 动态主题handler
 * @Description: 动态主题handler
 * @date 2021/10/14:22 下午
 * @since 1.0
 */
@Service
public class dynamicTopicHandler {


    private static List<String> topics;


    static {
        topics = new ArrayList<>();
    }



    public static List<String> getTopics(){
        if(Objects.nonNull(topics)){
            return topics;
        }else{
            return new ArrayList<>();
        }
    }


    public static List<MqttTopic> buildBaseTopics(String productId, String deviceId, String deviceName){
        List<MqttTopic> topics = new ArrayList<>();
        //基础属性topic
        topics.add(createBaseTopic( productId,  deviceId, "/properties/report", deviceName));
        //读取设备属性Topic
        topics.add(createBaseTopic( productId,  deviceId, "/properties/read", deviceName));
        //读取后设备属性消息Topic
        topics.add(createBaseTopic( productId,  deviceId, "/properties/read/reply", deviceName));
        //修改设备属性
        topics.add(createBaseTopic( productId,  deviceId, "/properties/write", deviceName));
        //回复设备属性
        topics.add(createBaseTopic( productId,  deviceId, "/properties/write/reply", deviceName));
        //更新固件消息
        topics.add(createBaseTopic( productId,  deviceId, "/firmware/upgrade", deviceName));
        //上报更新固件进度
        topics.add(createBaseTopic( productId,  deviceId, "/firmware/upgrade/progress", deviceName));
        //拉取固件更新信息
        topics.add(createBaseTopic( productId,  deviceId, "/firmware/pull", deviceName));
        //上报固件版本
        topics.add(createBaseTopic( productId,  deviceId, "/firmware/report", deviceName));
        //获取固件版本
        topics.add(createBaseTopic( productId,  deviceId, "/firmware/read", deviceName));
        //设备日志上报
        topics.add(createBaseTopic( productId,  deviceId, "/log", deviceName));
        //设备同步时间请求
        topics.add(createBaseTopic( productId,  deviceId, "/time-sync", deviceName));
        //平台回复设备时间
        topics.add(createBaseTopic( productId,  deviceId, "/time-sync/reply", deviceName));
        return topics;
    }



    /**
     * 创建基础mqtt 主题
     * @param productId
     * @param deviceId
     * @param suffix
     * @param deviceName
     */
    private static MqttTopic createBaseTopic(String productId, String deviceId, String suffix, String deviceName){
        String topic = "/"+productId +"/"+deviceId + suffix;
        topics.add(topic);
        MqttTopic mqttTopic = new MqttTopic();
        mqttTopic.setTopicCode(topic);
        mqttTopic.setTopicName(deviceName+suffix);
        mqttTopic.setDeviceCode(deviceId);
        mqttTopic.setUsed(true);
        mqttTopic.setUsed(Boolean.TRUE);
        return mqttTopic;
    }

    /**
     * 创建报警  topic
     * @param productId
     * @param deviceId
     * @param deviceName
     */
    public static List<MqttTopic> buildAlarmTopic(String productId, String deviceId, String deviceName){
        List<MqttTopic> list = new ArrayList<>();
        MqttTopic topic = createBaseTopic(productId, deviceId, "/event/alarm", deviceName);
        list.add(topic);
        return list;
    }

    /**
     * 创建事件mqtt Topic
     * @param productId
     * @param deviceId
     * @param deviceName
     */
    public static List<MqttTopic> buildEventTopic(String productId, String deviceId, String deviceName){
        List<MqttTopic> list = new ArrayList<>();
        list.add(createBaseTopic(productId, deviceId, "/function/invoke", deviceName));
        list.add(createBaseTopic(productId, deviceId, "/function/reply", deviceName));
        return list;
    }


}
